using B.S.Domain.Accounts;
using B.S.Domain.Accountss;
using B.S.Domain.BasicData.Factory_modeling.Production_line_management;
using B.S.Domain.BasicData.Factory_modeling.shop_management;
using B.S.Domain.BasicData.Factory_modeling.Station_management;
using B.S.Domain.BasicData.MaterialManagement.BOM;
using B.S.Domain.BasicData.MaterialManagement.Material_type;
using B.S.Domain.BasicData.MaterialManagement.MaterialGroups;
using B.S.Domain.BasicData.MaterialManagement.Product_material;
using B.S.Domain.BasicData.QualityManagement;
using B.S.Domain.BasicData.Skill_management;
using B.S.Domain.BasicData.SupplierManagement;
using B.S.Domain.CangKu;
using B.S.Domain.Date;
using B.S.Domain.Generateplan;
using B.S.Domain.InvoiceManagement;
using B.S.Domain.Node;
using B.S.Domain.Process.AssignattributesOneModel;
using B.S.Domain.Process.ProcessmaintenanceModel;
using B.S.Domain.Process.ProcessrouteName;
using B.S.Domain.Ticket;
using B.S.Domain.Warehousemanagement.Inventoryinquiry.FinishedProductWarehouseModels;
using B.S.Domain.Warehousemanagement.Inventoryinquiry.RawmaterialsModels;
using B.S.Domain.Warehousemanagement.Inventoryinquiry.RawmaterialsTemporarilyModel;
using B.S.Domain.Warehousemanagement.MaterialpreparationqueryModle;
using B.S.Domain.Warehousemanagement.Redcardmaterialcontrol.MaterialcontrolModel;
using B.S.Domain.基础数据.不良信息维护;
using B.S.Domain.基础数据.客户管理;
using B.S.Domain.AICustomerService;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Migrations;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
using Pomelo.EntityFrameworkCore.MySql.Migrations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using B.S.Domain;

namespace B.S.Infratrctrue
{
    /// <summary>
    /// 配置上下文
    /// </summary>
    public class SysDbcontext : DbContext
    {
        /// <summary>
        /// 配置EF上下文
        /// </summary>
        /// <param name="options">获取连接字符串</param>
        public SysDbcontext(DbContextOptions options) : base(options)
        {
        }
        
        public DbSet<Account> accounts { get; set; }

        public DbSet<DeptClass> deptClasses { get; set; }

        public DbSet<FactoryClass> factoryClasses { get; set; }

        public DbSet<LoginLogger> loginLoggers { get; set; }

        public DbSet<OrcanClass> orcanClasses { get; set; }

        public DbSet<Promission> promissions { get; set; }


        public DbSet<PromissRoles> promissRoless { get; set; }

        public DbSet<PromissTypes> promissTypess { get; set; }

        public DbSet<RoleEntty> roleEntties { get; set; }

        public DbSet<UserRole> userRoles { get; set; }
        
        // 质量管理相关
        public DbSet<ExamineDesign> ExamineDesigns { get; set; }
        public DbSet<ExamineProject> ExamineProjects { get; set; }
        public DbSet<ExamineTask> ExamineTasks { get; set; }
        public DbSet<ExamineType> ExamineTypes { get; set; }
        public DbSet<MaterialsExamine> MaterialsExamines { get; set; }
        
        // 物料管理相关
        public DbSet<Product_material> Product_materials { get; set; }
        public DbSet<BOM> BOMs { get; set; }
        public DbSet<Material_type> Material_types { get; set; }
        
        // 客户管理相关
        public DbSet<client> clients { get; set; }
        
        // 不良信息维护相关
        public DbSet<Bad_phenomenon> Bad_phenomenons { get; set; }
        public DbSet<Bad_type_table> Bad_type_tables { get; set; }
        
        // 供应商管理相关
        
        // 技能管理相关
        public DbSet<Skill> Skills { get; set; }
        
        // 工厂建模相关
        public DbSet<Production_line_management> Production_line_managements { get; set; }
        public DbSet<Station_management> Station_managements { get; set; }
        public DbSet<shop_management> shop_managements { get; set; }


        //工序维护
        public DbSet<Processmaintenance> Processmaintenance { get; set; }
        //工序路线
        public DbSet<Processroute> Processroute { get; set; }
        /// <summary>
        /// 分配属性一
        /// </summary>
        public DbSet<AssignattributesOne> AssignattributesOne { get; set; }
   
        //成品仓
        public DbSet<FinishedProductWarehouses> FinishedProductWarehouses { get; set; }
        //原材料仓
        public DbSet<Rawmaterialss> Rawmaterialss { get; set; }
        //原材暂收仓
        public DbSet<RawmaterialsTemporarily> RawmaterialsTemporarily { get; set; }
        //备料单查询
        public DbSet<Materialpreparationquery> Materialpreparationquery { get; set; }
        //红卡物料控制
        public DbSet<Materialcontrol> Materialcontrol { get; set; }
        //生产计划基础信息
        public DbSet<GenerateItems> GenerateItems { get; set; }
        //生产计划生产物品列表
        public DbSet<GenerateplanBasic> GenerateplanBasic { get; set; }
        public DbSet<shuXing> GetEntities { get; set; }
        public DbSet<Material_group> GetMaterial_Groups { get; set; }
        public DbSet<Supplier> Getsuppliers { get; set; }
        public DbSet<CangKuinfo> CangKuinfos { get; set; }
        public DbSet<CangTypeinfo> cangTypeinfos { get; set; }
        public DbSet<WorkOrder> WorkOrders { get; set; }
        public DbSet<CangKuQU> CangKuQUs { get; set; }
        public DbSet<DingDan> dingDans { get; set; }
        public DbSet<ChuWei> chuWeis { get; set; }
        public DbSet<Nodeinfo> Nodeinfos { get; set; }
        public DbSet<Settinginfo> Settinginfos { get; set; }
        public DbSet<ShenPiinfo> shens { get; set; }

        public DbSet<TimeClass> timeClasses { get; set; }
        public DbSet<RuKuinfo> shangpininfos { get; set; }

        // AI客服相关
        /// <summary>
        /// AI客服对话记录
        /// </summary>
        public DbSet<AIConversation> AIConversations { get; set; }

        /// <summary>
        /// AI客服知识库
        /// </summary>
        public DbSet<AIKnowledgeBase> AIKnowledgeBases { get; set; }

        /// <summary>
        /// AI客服配置
        /// </summary>
        public DbSet<AIServiceConfig> AIServiceConfigs { get; set; }
        /// <summary>
        /// 成品检验
        /// </summary>
 
        public DbSet<Finished_ProductTest> Finished_ProductTests { get; set; }
        public DbSet<ShenchanInfo> shenchanInfos { get; set; }
        public DbSet<RukuinfoLog> LogInfo { get; set; }

        // 发票管理相关
        public DbSet<SalesInvoice> SalesInvoices { get; set; }
        public DbSet<SalesInvoiceDetail> SalesInvoiceDetails { get; set; }
        public DbSet<InvoiceReceipt> InvoiceReceipts { get; set; }
        public DbSet<RuKuinfo> Rukuinfos { get; set; }
        public DbSet<Tel> Tels { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Account>().HasQueryFilter(e => e.isDel == false);
            modelBuilder.Entity<shop_management>().HasQueryFilter(e => e.isDel == false);
            modelBuilder.Entity<Production_line_management>().HasQueryFilter(e => e.isDel == false);
            modelBuilder.Entity<client>().HasQueryFilter(e => e.isDel == false);
            modelBuilder.Entity<Material_group>().HasQueryFilter(e => e.isDel == false);
            modelBuilder.Entity<RoleEntty>().HasQueryFilter(e => e.isDel == false);
            modelBuilder.Entity<OrcanClass>().HasQueryFilter(e => e.isDel == false);
            modelBuilder.Entity<Product_material>().HasQueryFilter(e => e.isDel == false);
            modelBuilder.Entity<FactoryClass>().HasQueryFilter(e => e.isDel == false);
            modelBuilder.Entity<CangKuinfo>().HasQueryFilter(e => e.isDel == false);
            modelBuilder.Entity<CangKuQU>().HasQueryFilter(e => e.isDel == false);
            modelBuilder.Entity<Supplier>().HasQueryFilter(e => e.isDel == false);
            //modelBuilder.Entity<Shangpininfo>().HasQueryFilter(e => e.State ==  0);
            // AI客服相关配置
            modelBuilder.Entity<AIConversation>(entity =>
            {
                entity.HasKey(e => e.ConversationId);
                entity.HasQueryFilter(e => e.isDel == false);
            });
            
            modelBuilder.Entity<AIKnowledgeBase>(entity =>
            {
                entity.HasKey(e => e.KnowledgeId);
                entity.HasQueryFilter(e => e.isDel == false);
            });
            
            modelBuilder.Entity<AIServiceConfig>(entity =>
            {
                entity.HasKey(e => e.ConfigId);
                entity.HasQueryFilter(e => e.isDel == false);
            });
            modelBuilder.Entity<BOM>()
                .HasKey(b => b.BomId);

            modelBuilder.Entity<BOM>()
               .Property(b => b.BomId)
               .ValueGeneratedOnAdd();

            modelBuilder.Entity<BOM>()
                .HasOne(b => b.Parent)
                .WithMany(b => b.Children)
                .HasForeignKey(b => b.ParentId)
                .OnDelete(DeleteBehavior.Restrict);//防止级联删除

            //配置BOM 索引
            modelBuilder.Entity<BOM>()
                .HasIndex(b => b.Materialcode)
                .IsUnique();
            modelBuilder.Entity<BOM>()
                .HasIndex(b=>b.ParentId);
            modelBuilder.Entity<BOM>()
                .HasIndex(b => b.LevelId);

            // 发票管理相关全局查询过滤器
            modelBuilder.Entity<SalesInvoice>().HasQueryFilter(e => e.isDel == false);
            modelBuilder.Entity<SalesInvoiceDetail>().HasQueryFilter(e => e.isDel == false);
            modelBuilder.Entity<InvoiceReceipt>().HasQueryFilter(e => e.isDel == false);
                
        }

    }
}
